package com.bayss.core.service;

import com.bayss.core.entity.user.TransactionRecord;
import com.bayss.core.entity.user.TransactionRecordExample;
import com.bayss.core.entity.user.UserInfo;
import com.bayss.core.entity.user.UserInfoExample;
import com.bayss.core.entity.user.mapper.TransactionRecordMapper;
import com.bayss.core.entity.user.mapper.UserInfoMapper;
import com.bayss.core.model.TransactionCount;
import com.bayss.core.util.DokoDateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LDL on 2020/1/9.
 */
@Service
public class TransactionRecordService {
    @Autowired
    private TransactionRecordMapper transactionRecordMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    /**
     * 交易查询
     *
     * @param transactionType 交易类型
     * @param startTime       yyyy-MM-dd HH:mm:ss
     * @return
     */
    public PageInfo<TransactionRecord> list(Long uid, Integer transactionType,String cellphone, String startTime, String endTime, Integer page, Integer pageSize) {
        TransactionRecordExample example = new TransactionRecordExample();
        example.setOrderByClause("create_time desc");
        TransactionRecordExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        if (StringUtils.isNotBlank(cellphone)) {
            UserInfoExample userInfoExample = new UserInfoExample();
            userInfoExample.createCriteria().andCellphoneEqualTo(cellphone);
            List<UserInfo> userInfoList = userInfoMapper.selectByExample(userInfoExample);
            if (userInfoList != null && userInfoList.size() > 0) {
                uid = userInfoList.get(0).getId();
            }
        }
        if (uid != null) {
            criteria.andUidEqualTo(uid);
        }
        if (transactionType != null) {
            criteria.andTransactionTypeEqualTo(transactionType.byteValue());
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            String dateFormat = "yyyy-MM-dd HH:mm:ss";
            Date start = DokoDateUtil.formatDate(startTime, dateFormat);
            Date end = DokoDateUtil.formatDate(endTime, dateFormat);
            criteria.andCreateTimeBetween(start, end);
        }

        PageInfo<TransactionRecord> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> transactionRecordMapper.selectByExample(example));

        for (TransactionRecord record : result.getList()) {
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(record.getUid());
            if (userInfo == null) continue;
            record.setNickName(userInfo.getNickname());
            record.setCellphone(userInfo.getCellphone());
        }
        return result;
    }


    /**
     * 保存订单交易记录
     */
    public void saveOrderRecord(Long uid, Integer amount, String orderNo) {
        try {
            TransactionRecord record = new TransactionRecord();
            record.setUid(uid);
            record.setAmount(amount);
            record.setOrderNo(orderNo);
            record.setRemark("消费");
            record.setTransactionType(Byte.valueOf("2"));
            record.setCreateTime(new Date());
            record.setStatus(1);
            transactionRecordMapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public TransactionCount countRecord(Long uid, String startTime, String endTime) {
        TransactionRecordExample example = new TransactionRecordExample();
        TransactionRecordExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        if (uid != null) {
            criteria.andUidEqualTo(uid);
        }
        if (startTime != null && endTime != null) {
            String dateFormat = "yyyy-MM-dd HH:mm:ss";
            Date start = DokoDateUtil.formatDate(startTime, dateFormat);
            Date end = DokoDateUtil.formatDate(endTime, dateFormat);
            criteria.andCreateTimeBetween(start, end);
        }

        int incomeTotal = 0;
        int totalPay = 0;
        List<TransactionRecord> list = transactionRecordMapper.selectByExample(example);
        for (TransactionRecord r : list) {
            if (r.getTransactionType().intValue() == 1) {
                incomeTotal += r.getAmount();
            } else if (r.getTransactionType().intValue() == 2) {
                totalPay += r.getAmount();
            }
        }
        return TransactionCount.builder().totalIncome(incomeTotal).totalPay(totalPay).build();
    }

}
